package org.example.everyday;

import org.example.ch04.tree.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * <p>
 *
 * @author anlu
 * @date 2022/6/24 13:55
 */
public class Lc515 {
    public static void main(String[] args) {
        Lc515 lc515 = new Lc515();

        TreeNode node1 = new TreeNode(2,null,null);
        TreeNode node2 = new TreeNode(3);


        TreeNode root = new TreeNode(8,node1,node2);
        List<Integer> res = lc515.largestValues(root);
        System.out.println(res);


    }

    public List<Integer> largestValues(TreeNode root) {
        if(root == null){
            return new ArrayList<Integer>();
        }
        List<Integer> res = new ArrayList<Integer>();
        dfs(res,root,0);
        return res;

    }

    private void dfs(List<Integer> res, TreeNode root, int curHeight) {
        if(curHeight == res.size()){
            res.add(root.val);
        }else{
            res.set(curHeight,Math.max(res.get(curHeight),root.val));
        }
        if(root.left != null){
            dfs(res,root.left,curHeight + 1);
        }
        if(root.right != null){
            dfs(res,root.right,curHeight + 1);
        }
    }

}
